package com.example.emos.wx.controller.user;

import cn.hutool.core.bean.BeanUtil;
import com.example.emos.wx.entity.TbRole;
import com.example.emos.wx.pojo.dto.TestSayHelloForm;
import com.example.emos.wx.pojo.dto.UserLoginDTO;
import com.example.emos.wx.pojo.dto.UserRegisterDTO;
import com.example.emos.wx.pojo.vo.UserLoginVO;
import com.example.emos.wx.service.UserService;
import com.example.emos.wx.utils.JwtUtil;
import com.example.emos.wx.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Validated
@Api(tags = "测试接口文档")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("hello")
    @ApiOperation("测试接口")
    public HashMap<String, Object> hello() {
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("name", "chentian");
        obj.put("age", 30);
        return obj;
    }

    @PostMapping("/sayHello")
    @ApiOperation("最简单的测试方法")
    public R sayHello(@Validated @RequestBody TestSayHelloForm form) {
        return R.success().put("message", "Hello," + form.getName());
    }

    @PostMapping("/login")
    @ApiOperation("登录")
    public R login(@Validated @RequestBody UserLoginDTO userLoginDTO) {
        UserLoginVO userLoginVO = userService.login(userLoginDTO);
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(userLoginVO);
        return R.success("登录成功", stringObjectMap);
    }

    @PostMapping("/register")
    @ApiOperation("注册")
    public R register(@Validated @RequestBody UserRegisterDTO userRegisterDTO) {
        userService.registerUser(userRegisterDTO);
        return R.success();
    }
}
